import Vue from 'vue'
import VueRouter from 'vue-router'
//import Home from '../views/Home.vue'

Vue.use(VueRouter)

const baseRouter = [
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404.vue')
  }
]

const routes = [
 {
    path: '/login',
    name: 'login',
    component: () => import('@/login.vue')
  },{
    path: '/main',
    name: 'main',
    component: () => import('@/App.vue')
  },{
    path: '/menu',
    name: 'menu',
    component: () => import('@/components/header.vue'),
    children: baseRouter
  }
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
/**
 * 导航守卫，跳转 登录/404
 */
router.beforeEach((to, from, next) => {
  //检查vuex中登录状态，未登录则直接导向login
  if (Vue.prototype.$isEmpty(router.app.$options.store.state.token)){
    if(to.path==='/login') { //如果是登录页面路径，就直接next()
      next();
    }else {
      next('/login')
    }
  }else if (to.matched.length === 0) {
    next('/404');
  } else {
    //如果匹配到就跳转
    next();
  }
})

export default router
export {
  baseRouter
}
